//
//  ProDetailMessageCell.m
//  LazyCat
//
//  Created by Abner on 16/4/22.
//  Copyright (c) 2016年 zhanshu. All rights reserved.
//

#import "ProDetailMessageCell.h"
#import "AddProduct.h"
#import "YYText.h"

@implementation ProDetailMessageCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.proMesTitleLB = [[UILabel alloc]init];
        [self.contentView addSubview:self.proMesTitleLB];
        self.proMesTitleLB.font = [UIFont boldSystemFontOfSize:14];
        self.proMesTitleLB.text = @"商品信息";
        self.proMesTitleLB.frame = CGRectMake(10, 10, ScreenWidth-20, 14);
        self.proMesTitleLB.textColor = SetColorRGBValue(0x000000, 1.0);
        
        self.proGuiGeLB = [[UILabel alloc]init];
        [self.contentView addSubview:self.proGuiGeLB];
        self.proGuiGeLB.font = FontSet(13);
        self.proGuiGeLB.textColor = SetColorRGBValue(0x000000, 1.0);
        self.proGuiGeLB.text = [NSString stringWithFormat:@"【商品规格】"];

        
        self.proGuiGeLB1 = [YYLabel new];
        [self.contentView addSubview:self.proGuiGeLB1];
        self.proGuiGeLB1.textColor = SetColorRGBValue(0x000000, 0.5);
        
        self.proIntroductLB = [[UILabel alloc]init];
        [self.contentView addSubview:self.proIntroductLB];
        self.proIntroductLB.font = FontSet(13);
        self.proIntroductLB.textColor = SetColorRGBValue(0x000000, 1.0);
        self.proIntroductLB.text = [NSString stringWithFormat:@"【商品介绍】"];
        
        self.proIntroductLB1 = [YYLabel new];
        [self.contentView addSubview:self.proIntroductLB1];
        self.proIntroductLB1.textColor = SetColorRGBValue(0x000000, 1.0);
    }
    return self;
}
-(void)setupProDetailMessageCellData:(AddProduct *)aproDetailModel
{
    _aproDetailModel = aproDetailModel;
    //处理数据加载慢或者网络差时，“商品介绍”会和“商品规格重”
    if (aproDetailModel.sdescription.length > 0) {
        self.proIntroductLB.hidden = NO;
        self.proIntroductLB1.hidden = NO;
    }else{
        self.proIntroductLB.hidden = YES;
        self.proIntroductLB1.hidden = YES;
    }
    
    CGFloat introductH = 0;
    if (aproDetailModel.productspec.count > 1) { //【商品详情】中不显示规格
        self.proGuiGeLB.hidden = YES;
        self.proGuiGeLB1.hidden = YES;
        introductH = self.proMesTitleLB.bottom;
    }else{
        self.proGuiGeLB.hidden = NO;
        self.proGuiGeLB1.hidden = NO;

        NSString *specStr = @"";
        if (aproDetailModel.productspec.count == 1) { //规格元素只有一个[多规格的话是在上面以标签的形式显示]
            LCProductSpec *aSpec = [[LCProductSpec alloc]init];
            aSpec = aproDetailModel.productspec[0];
            if (aSpec.specname.length > 0) {
                specStr = aSpec.specname;
            }
        }
        //商品规格
        self.proGuiGeLB.frame = CGRectMake(5, 34, 80, 12);
        CGFloat guigeH = [self getMessageHeight:specStr andLabel:self.proGuiGeLB1];
        self.proGuiGeLB1.frame = CGRectMake(85, 32, ScreenWidth-100, guigeH);
        introductH = self.proGuiGeLB1.bottom;
    }
    
    //商品介绍
    NSString *introStr = @"";
    self.proIntroductLB.frame = CGRectMake(5, introductH+10, 80, 12);
    if (aproDetailModel.sdescription.length > 0) {
        introStr = aproDetailModel.sdescription;
    }
    CGFloat introHeight = [self getMessageHeight:introStr andLabel:self.proIntroductLB1];
    self.proIntroductLB1.frame = CGRectMake(85, introductH+9, ScreenWidth-100, introHeight);


}
/**
 *  获取lb的高度（默认字体13，行间距8，lb宽ScreenWidth-100）
 *  @param mess lb.text
 *  @param lb (YYLabel *)label
 *  @return lb的高度
 */
-(CGFloat)getMessageHeight:(NSString *)mess andLabel:(YYLabel *)lb
{
    NSMutableAttributedString *introText = [[NSMutableAttributedString alloc] initWithString:mess];
    introText.yy_font = FontSet(13);
    introText.yy_lineSpacing = 8;
    lb.attributedText = introText;
    CGSize introSize = CGSizeMake(ScreenWidth-100, CGFLOAT_MAX);
    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:introSize text:introText];
    lb.textLayout = layout;
    CGFloat introHeight = layout.textBoundingSize.height;
    return introHeight;
}

+(instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *indetifier = @"ProDetailMessageCellIdentifier";
    ProDetailMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:indetifier];
    if (!cell) {
        cell = [[ProDetailMessageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indetifier];
    }
    return cell;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
-(void)setFrame:(CGRect)frame
{
    frame = CGRectMake(frame.origin.x, frame.origin.y+5, frame.size.width, frame.size.height-5);
    [super setFrame:frame];
    
}
-(CGFloat)getHeight:(NSString *)str fontSize:(CGFloat)fontSize strwidth:(CGFloat)strwidth{
    CGSize aSize = [str boundingRectWithSize:CGSizeMake(strwidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} context:nil].size;
    return aSize.height;
}
@end
